package com.itheima.reggie.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.config.common.R;
import com.itheima.reggie.entity.dto.CategoryDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name CategoryController
 * @Author BaiXin
 * @Date 2022-10-31 10:19
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> updateById(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteById(Long id) {

        return categoryService.deleteById(id);
    }

    /**
     * 分页查询菜品
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R findPage(Integer page, Integer pageSize) {
        IPage iPage = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = Wrappers.<Category>lambdaQuery()
                .orderByAsc(Category::getSort);
        return R.success(categoryService.page(iPage, lambdaQueryWrapper));
    }

    /**
     * 新增菜品及套餐
     *
     * @param categoryDto
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody CategoryDto categoryDto) {
        log.info("category:{}", categoryDto);
        categoryService.save(categoryDto);
        return R.success("新增成功");
    }

    /**
     * 根据类型查询菜品或者套餐
     * @param type
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> findByType(Integer type){
        //查询
        List<Category> dishes = categoryService.findByType(type);
        //处理结果返回
        return R.success(dishes);
    }

}
